#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using Thn.Application.Configuration;
using Thn.Communication.Teleporting;
using Thn.Communication.Teleporting.Settings;
using Thn.Communication.Demo.SampleBusiness;
#endregion

namespace Thn.Communication.Teleporting.Connectors.Loopback
{
    /// <summary>
    /// Sample connector to make loopback. This is useful for debugging.
    /// </summary>
    public class LoopbackConnector : ITeleportConnector
    {
        #region Initialize
        /// <summary>
        /// Setup connection based on the provided configuration
        /// </summary>
        public void Initialize(ConfigSection section)
        {
            //Get teleporter object
            Teleporter tp = (Teleporter)Teleport.Teleporter;

            //Intercept send message event to process
            tp.SendMessage += new TeleportRequestHandler(tp_SendMessage);

            //Add gateway for server-side
            Teleport.AddGateway("Business", new BusinessObject());
        }
        #endregion

        #region Teleport - Send message
        string tp_SendMessage(string gateway, string method, string encodedArgs)
        {
            //loopback to Teleport
            return Teleport.Teleporter.ProcessRequest(gateway, method, encodedArgs);
        }
        #endregion

        #region Apply Configuration
        /// <summary>
        /// Read parameters from configuration.
        /// </summary>
        void ApplyConfiguration(ConfigSection section)
        {
            //loopback does not require any configurations.
        }
        #endregion

        #region ITeleportConnector Members

        #region Teleporter
        private ITeleporter mTeleporter;
        /// <summary>
        /// Gets/Sets the teleporter this connector shall be connecting for.
        /// <para>When unassigned, the connector shall use the default teleporter.</para>
        /// </summary>
        public ITeleporter Teleporter
        {
            get
            {
                ITeleporter result = null;
                if (mTeleporter != null) result = mTeleporter;
                else result = Teleport.Teleporter;

                return result;
            }
            set { mTeleporter = value; }
        }
        #endregion

        #region Configuration
        private ConfigSection mConfiguration;
        /// <summary>
        /// Gets/Sets a custom configuration for initialization.
        /// </summary>
        public ConfigSection Configuration
        {
            get { return mConfiguration; }
            set
            {
                mConfiguration = value;
                if (mConfiguration != null) ApplyConfiguration(mConfiguration);
                else Thn.Exceptions.NullArgumentException.Publish("Configuration");
            }
        }
        #endregion

        #region Initialize
        /// <summary>
        /// Prepare this connector.
        /// </summary>
        public void Initialize()
        {
            //Get teleporter object
            Teleporter tp = (Teleporter)this.Teleporter;

            //Intercept send message event to process by sending the message to remoting
            tp.SendMessage += new TeleportRequestHandler(tp_SendMessage);
        }
        #endregion

        #endregion
    }
}
